package com.zahb.qadx.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatImageView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;
import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseActivity;
import com.zahb.qadx.base.BaseApplication;
import com.zahb.qadx.model.AttentionModel;
import com.zahb.qadx.model.CommonResponse;
import com.zahb.qadx.net.RetrofitService;
import com.zahb.qadx.ui.adapter.CourseAdapter;
import com.zahb.qadx.ui.fragment.PersonIntroduceFragment;
import com.zahb.qadx.ui.fragment.RecommendedCourseFragment;
import com.zahb.qadx.util.ImageLoaderKt;
import com.zahb.qadx.util.Tips;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by huwei.
 * Data 2020-10-18.
 * Time 21:03.
 */
public class AttentionDetailActivity extends BaseActivity {
    @BindView(R.id.iv_icon)
    AppCompatImageView ivIcon;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_desc)
    TextView tvDesc;
    @BindView(R.id.tv_attenttion)
    TextView tvAttenttion;
    @BindView(R.id.img_attention)
    ImageView imgAttention;
    private ViewPager courseViewpager;
    private TabLayout courseTablayout;
    private List<String> courseTablist;
    private List<Fragment> courseVpList;
    private CourseAdapter pagerAdapter;
    FragmentManager fragmentManager;
    private Gson mGson = new Gson();
    String relationId;
    @Override
    protected int getLayout() {
        return R.layout.activity_attention_detail;
    }

    @Override
    protected void initViews() {
        mUnbinder = ButterKnife.bind(this);
        ImageLoaderKt.loadImageCircle(ivIcon, getIntent().getStringExtra("img"));//图像
        tvName.setText(getIntent().getStringExtra("name"));//姓名
        tvDesc.setText(getIntent().getStringExtra("desc"));//描述
        relationId = getIntent().getStringExtra("relationId");
        courseTablayout = findViewById(R.id.tab_course);
        courseViewpager = findViewById(R.id.vp_course);
        courseTablist = new ArrayList<>();
        courseVpList = new ArrayList<>();
        courseTablist.add("个人介绍");
        courseTablist.add("推荐课程");
        PersonIntroduceFragment personIntroduceFragment = new PersonIntroduceFragment();
        RecommendedCourseFragment recommendedCourseFragment = new RecommendedCourseFragment();
        personIntroduceFragment.setParamse(relationId);
        Bundle bundle = new Bundle();
        bundle.putString("desc", getIntent().getStringExtra("desc"));
        bundle.putString("cateName", getIntent().getStringExtra("cateName"));//擅长
        bundle.putString("certificates", getIntent().getStringExtra("certificates"));//资质
        personIntroduceFragment.setArguments(bundle);
        recommendedCourseFragment.setParamse(relationId);
        courseVpList.add(personIntroduceFragment);
        courseVpList.add(recommendedCourseFragment);
        fragmentManager = getSupportFragmentManager();
        pagerAdapter = new CourseAdapter(fragmentManager, courseVpList, courseTablist);
        courseViewpager.setAdapter(pagerAdapter);
        courseTablayout.addTab(courseTablayout.newTab().setText(courseTablist.get(0)));
        courseTablayout.addTab(courseTablayout.newTab().setText(courseTablist.get(1)));
        courseTablayout.setupWithViewPager(courseViewpager);
    }

    @Override
    protected int getTitleStringRes() {
        return R.string.my_attention_detial;
    }

    @OnClick({R.id.img_attention})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_attention:
                Map<String, Object> params = new HashMap<>();
                params.put("attentioType", 1); //关注类型 1讲师
                params.put("id", 1); //关注列表id
                params.put("relationId", relationId); // 管理id
                params.put("type", 0); //关注请求类型 0关注 1 取消关注
                String json = mGson.toJson(params);
                RequestBody body = RequestBody.create(MediaType.parse("application/json"), json);
                if (!TextUtils.isEmpty(BaseApplication.getContext().getToken())) {
                    showProgressDialog("");
                    addDisposable(RetrofitService.getNetService()
                            .getSaveorCancel(body)
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(response -> {
                                hideProgressDialog();
                                getAttentionSuccessOrErr(response);
                            }, throwable -> {
                                hideProgressDialog();
                                throwable.printStackTrace();
                                Tips.RequestError(getActivity());
                            }));
                }
                break;
        }
    }

    private void getAttentionSuccessOrErr(CommonResponse<Object> response) {
        if (response.getStatusCode() == 200) {
            imgAttention.setImageDrawable(getResources().getDrawable(R.mipmap.give_red));
            tvAttenttion.setText(R.string.has_attention);
            imgAttention.setEnabled(false);
        } else {
            showBindToast(response.getErrorInfo());
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        Map<String, Object> params = new HashMap<>();
        params.put("attentioType", "1");
        params.put("pageNo", 1);
        params.put("pageSize", 10);
        params.put("relationId", "");
        String json = mGson.toJson(params);
        RequestBody body = RequestBody.create(MediaType.parse("application/json"), json);
        if (!TextUtils.isEmpty(BaseApplication.getContext().getToken())) {
            showProgressDialog("");
            addDisposable(RetrofitService.getNetService()
                    .getUserAttention(body)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(response -> {
                        hideProgressDialog();
                        getDetailData(response);
                    }, throwable -> {
                        hideProgressDialog();
                        throwable.printStackTrace();
                        Tips.RequestError(getActivity());
                    }));
        }
    }

    private void getDetailData(CommonResponse<AttentionModel.AttentionData> response) {
        if (response.getStatusCode() == 200) {
            List<AttentionModel.AttentionData.AttentionList> list = response.getData().getList();
            for (int i =0; i < list.size(); i++){
                if(list !=null){
                    if(relationId.equals(list.get(i).getRelationId())){
                        imgAttention.setImageDrawable(getResources().getDrawable(R.mipmap.give_red));
                        tvAttenttion.setText(R.string.has_attention);
                        imgAttention.setEnabled(false);
                    }
//                   list.get(i).getRelationId();
//                    imgAttention.setImageDrawable(getResources().getDrawable(R.mipmap.give_red));
//                    tvAttenttion.setText(R.string.has_attention);
//                    imgAttention.setEnabled(false);
//                    String relationId = list.get(i).getRelationId();
//                    String title = list.get(i).getTitle();
//                    String name = list.get(i).getName();
//                    String headPhoto = list.get(i).getHeadPhoto();
//                    tvName.setText(list.get(i).getName());
//                    tvDesc.setText(list.get(i).getTitle());
//                    type = String.valueOf(teacherModel.getData().getType());
                }
            }
//            if (list.size() > 0){
//                imgAttention.setImageDrawable(getResources().getDrawable(R.mipmap.give_red));
//                tvAttenttion.setText(R.string.has_attention);
//                imgAttention.setEnabled(false);
//
//                tvName.setText(teacherModel.getData().getName());
//                tvDesc.setText(teacherModel.getData().getTitle());
//                type = String.valueOf(teacherModel.getData().getType());
//            }
        }
    }

}